package com.scanner.hardware.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;

import com.scanner.barcodeservice.SerialPortService;
import com.scanner.bean.CodeMulit;
import com.scanner.bean.CodeMulitContent;
import com.scanner.bean.MotoBarcode;
import com.scanner.bean.MotoQRBarcode;
import com.scanner.hardware.R;
import com.scanner.hardware.db.helper.DaoHelper;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;

import java.util.List;

public class MotoAllCodeAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity,BaseViewHolder> {

    public static final int TYPE_LEVEL_0 = 0;
    public static final int TYPE_LEVEL_1 = 1;
    private final DaoHelper daoHelper;
    private Context context;

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public MotoAllCodeAdapter(List<MultiItemEntity> data, Context context) {
        super(data);
        addItemType(TYPE_LEVEL_0, R.layout.item_code_head);
        addItemType(TYPE_LEVEL_1, R.layout.item_motosiger_layout);
        this.context = context;
        daoHelper = new DaoHelper(context);
    }

    @Override
    protected void convert(final BaseViewHolder helper, MultiItemEntity item) {
        switch (helper.getItemViewType()){

            case TYPE_LEVEL_0:
                final CodeMulit mulit = (CodeMulit) item;
                helper.setText(R.id.tv_adapter_title,mulit.getTitle());
                helper.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int pos = helper.getAdapterPosition();
                        if (mulit.isExpanded()){
                            collapse(pos);
                        } else {
                            expand(pos);
                        }
                    }
                });
                break;
            case TYPE_LEVEL_1:
                final CodeMulitContent content = (CodeMulitContent) item;
                helper.setText(R.id.tv_sigName,content.getCodeName());
                int codeState = content.getCodeState();
                if(codeState == 1){
                    helper.setChecked(R.id.cb_choose,true);
                }else if(codeState == 0){
                    helper.setChecked(R.id.cb_choose,false);
                }
                helper.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int codeState = content.getCodeState();
                        String codeName = content.getCodeName();
                        MotoBarcode motoBean = daoHelper.getMotoBean(codeName);
                        MotoQRBarcode qrBean = daoHelper.getQRBean(codeName);
                        if(codeState == 1){//打开
                            content.setCodeState(0);
                            if(qrBean != null){
                                qrBean.setQRcodeState(0);
                                daoHelper.upDateQRCheck(qrBean);
                            }
                            if(motoBean != null){
                                motoBean.setCodeState(0);
                                daoHelper.updateSigCheck(motoBean);
                            }
                            sendBrocastUpdate(content.getCodeClose());
                        }else if(codeState == 0){//关闭
                            content.setCodeState(1);
                            if(qrBean != null){
                                qrBean.setQRcodeState(1);
                                daoHelper.upDateQRCheck(qrBean);
                            }
                            if(motoBean != null){
                                motoBean.setCodeState(1);
                                daoHelper.updateSigCheck(motoBean);
                            }
                            sendBrocastUpdate(content.getCodeOpen());
                        }

                        notifyDataSetChanged();

                    }
                });
                break;
        }
    }

    private void sendBrocastUpdate(byte[] value){
        Intent intent = new Intent(SerialPortService.BARCODEPORT_WRITEDATA_ACTION);
        intent.putExtra(SerialPortService.BARCODEPORT_WRITER_WXTAR_BYTRDATA, value);
        context.sendBroadcast(intent);
    }
}
